package com.cpvideo.activities;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.text.TextUtils;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;
import com.cpvideo.R;
import com.cpvideo.video.JZMediaIjkplayer;
import icangyu.base.utils.ImageLoader;


/**
 * Created by blensmile on 09/08/2017.
 */
public class VideoActivity extends AppCompatActivity {

    JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;
    SensorManager sensorManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        String url = getIntent().getStringExtra("url");
        String cover= getIntent().getStringExtra("cover");
        if(TextUtils.isEmpty(url)) {
            onBackPressed();
        }
        String proxyUrl = null;
            proxyUrl = url;
//            HttpProxyCacheServer proxy = App.getProxy(this);
//            proxyUrl = proxy.getProxyUrl(url);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();
//        if(url.endsWith("mov")){
            JZVideoPlayer.setMediaInterface(new JZMediaIjkplayer());
//        }else {
//            JZVideoPlayer.setMediaInterface(new JZMediaSystem());
//        }
        JZVideoPlayerStandard jcVideoPlayerStandard = (JZVideoPlayerStandard) findViewById(R.id.videoPlayer);
        JZVideoPlayer.WIFI_TIP_DIALOG_SHOWED = true;
        jcVideoPlayerStandard.setUp(proxyUrl
                , JZVideoPlayerStandard.SCREEN_STATE_OFF,"");

        if(TextUtils.isEmpty(cover)) {
            jcVideoPlayerStandard.thumbImageView.setImageResource(R.drawable.live_root);
        }else {
            ImageLoader.showOrFile(this, jcVideoPlayerStandard.thumbImageView, cover,R.drawable.live_root,2);
        }

//        jcVideoPlayerStandard.batteryTimeLayout.setVisibility(View.GONE);
//        jcVideoPlayerStandard.fullscreenButton.setVisibility(View.GONE);
//        jcVideoPlayerStandard.loop = true;
        jcVideoPlayerStandard.startButton.performClick();
    }





    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        try {
            sensorManager.unregisterListener(sensorEventListener);
            JZVideoPlayer.releaseAllVideos();
//        }catch (IllegalStateException e){
//            e.printStackTrace();
//        }
    }
}
